package com.macro.mall.demo.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：尚志豪
 * @Package：com.macro.mall.demo.controller
 * @Project：mall-swarm-B
 * @name：PayController
 * @Date：2024/10/31 10:12
 */
@RestController
@RequestMapping("/alipay")
public class PayController {

    // 商户appid
    private static String APPID = "9021000141659844";
    // 私钥 pkcs8格式的
    private static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCFV/KGwqOf+kZQP7wHXIuWAqWHOkHxGRrpFHJptHVyI15ed7+yO/o+iaYr7EdrpBkSAdu+vn1mS0GoBQ73EvfeVdyQg7d+KRbZ+QaR9L2QP9/KjC1ooDPOQqifnOoOLvu8IDwsw0MwTHArKNFR7e0ehzkGiLS3PAwxdC6r1E+K1Br9BCwqNVu/23tfizVAhf0VX8IGPqt6gNPSJ7frCLWPvSjR2M2Cxsg297i8eufSz6Sg6IxW21Rgfwk/x/Ri+fhEnCBu5hjGMDZlodG4zxQih4U7Txo7uwq6x9qFOoCdI4oW15n6Y6pNdmRDwqbmlhu9vFh41xkTL+jk8tL/dCdnAgMBAAECggEAWebz83vjigbZxV1TTrtMuXVLWSMkNoOwqxlIEpDC5pOGGrcuHeNmUyfHS7HgbWiUJpnuKGqSbZeryrauYlL9n3/QV/cWjg7yA0Bs95dE/66xGgTs0K/CffxfaE4JWErBGxO4vo1gDX5b2h2JE24RAxY0kn01Kp1nsygcF2nrvzNMRjWDPUT0DgFgbpDfym1elWPD3TFQ6ZD3YMQv2ZPnm3flGNsdXJuYf8d3pQUcZjAvxcO5WRuOhsRAti4ax127KWlsUKpM5oi5Gl8LylsZQHLu6MG6RGazr2zaw8f/IMArLYh6Hj/7dL0RXM0bKjWotV78AKMhu44vAGG5Zc6ywQKBgQDLFIf6tZKWNDfR6eEU9uYKEssgas7ICfdCV36CvjqiZroTme8cHWbz7KvOfNBF+YjwNwvCjsRh04Af8sGfbSVPHEtaZIqWjL2gmKUXOe0VTLSC36+dEuifPA2aukxJELczCaABjhQRmdlwzfGB9Y3q0zRgr1E4FHkj1GntOAUDNwKBgQCoF0ms5y0MPAmMGPgzJ+kPTVQp1Z8+1fNp3QpUXp7cadiogFI4sR2Q8q8+0nlpkqjH0pqMXRkdzfknygNZLl/JkoWl/T+hyJmr5zJvYMv7ULgMkXRL+1vqTnxz3NoPrwEHnDsJ87hw2TMTl8DyAeMJX8A3eUAnzS2qJ8lTHsp1UQKBgQCBTMYzU25Moly0E2RTE8xtt3+LWVXlOasrm0eaeQIeKqfpGQaL8RB+2D6fjwqQswuEXpfnPT0+SAp4zPiIyFaL4yj7Ga6/okaOPQJIIoKYICmCdmd66/IeiH7k+B7gGnt6THOK6epPNpSiZQ2SwESmsejUcmuU+DwVGwaCmjteywKBgEhzgTYdjrUvS44vVQv3r+UAl75QbYtP/NMsiz9CaYbnuJMofEgjT3Mip5aUcfCnXkiC5kqJ9kF6fmJAGKFf87mWhjS9q6e4J0SJKpwHROTWLf5803vMpBmiKtJl5zfLXs5A8NGhU2aQuAtB5dBqL8nik59Vi+mb7orhC03gf7OBAoGAFFiZemW+UvCn+qEWHSrivTkLnL0s5LvjZV7eBqSndI2wWri4ooICDaHz90puIpjz1KKW7rl0eM1R2ufKUIXNrC1XY0+sxDfdbDy3cP+Hz5KGA6i0KuvRSa3RKL1bjNrWo1aL8g7iNrr84ReW5HsLyKHm6mkuzoAcl4LWH3AOSxU=";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    private static String return_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/return_url.jsp";
    // 请求网关地址
    private static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 编码
    private static String CHARSET = "UTF-8";
    // 返回格式
    private static String FORMAT = "json";
    // 支付宝公钥
    private static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgAmJjgNURDO1tCEyoD5mWtfJ6O1v5/MaHGgpgfvEDkAB4esQy/JAMBBWXk53syCPeDJTU3CHQraVEKg932KPVzoknRqOWcytQ8dizUjZuS6iN1Qmoln5hzCW6n1Kyh0WTPRbnAVDiBj8l5i+RclR7l+irYIQIuZZ+LLxtqWwgKzkFBYwi42hBx+9EZMlN3mVcsY6WZtI75ZCEQNYZNXSJ5j5D/oK6Q5jaA4ZQ3c1gqvK1o7VT/vt9oY7fOKxsqTuqQfvHHkqbN5BtbIfaHvGFZuf8yQCSa99i2CqbGrzoF443R/2OuObLbZcfSudF1H+4Fk5xq80XUs47njPWUxxyQIDAQAB";
    // 日志记录目录
    private static String log_path = "/log";
    // RSA2
    private static String SIGNTYPE = "RSA2";

    @PostMapping("/pay")
    public void pay(){
        String out_trade_no = "4651541654220";
        String subject = "1111111111";
        System.out.println(subject);
        String total_amount = "500";
        String body = "";
        String timeout_express= "2m";
        String product_code = "QUICK_WAP_WAY";

        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setNotifyUrl(notify_url);
        alipay_request.setReturnUrl(return_url);

        String form = "";
        try {
            form = client.pageExecute(alipay_request).getBody();
            System.out.println(form);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

    }

}
